package team.yidong.fenda.serviceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.yidong.fenda.DTO.KnowledgeDTO;
import team.yidong.fenda.DTO.KnowledgeDTOForSys;
import team.yidong.fenda.entity.Knowledge;
import team.yidong.fenda.entity.KnowledgeImg;
import team.yidong.fenda.repository.KnowLedgeImgRepository;
import team.yidong.fenda.repository.KnowledgeRepository;
import team.yidong.fenda.service.KnowledgeService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuanlong on 2018/3/7.
 */
@Service
public class KnowledgeServiceImpl implements KnowledgeService {
    @Autowired
    KnowledgeRepository knowledgeRepository;
    @Autowired
    KnowLedgeImgRepository knowLedgeImgRepository;

    @Override
    public JSONArray findKnowledgeByHospitalId(Integer hospitalId) {
        List<KnowledgeDTOForSys> knowledgeDTOForSysList = new ArrayList<>();
        List<Knowledge> knowledgeList = knowledgeRepository.findByHospitalId(hospitalId);
        for (Knowledge knowledge : knowledgeList) {
            KnowledgeDTOForSys knowledgeDTOForSys = new KnowledgeDTOForSys();
            knowledgeDTOForSys.setKnowledgeId(knowledge.getId());
            knowledgeDTOForSys.setContent(knowledge.getContent());
            knowledgeDTOForSys.setHeadline(knowledge.getHeadline());
            List<KnowledgeImg> knowledgeImgList = knowLedgeImgRepository.findByKnowledgeId(knowledge.getId());
            knowledgeDTOForSys.setKnowledgeImgList(knowledgeImgList);
            knowledgeDTOForSysList.add(knowledgeDTOForSys);
        }
        JSONArray jsonArray = JSONArray.fromObject(knowledgeDTOForSysList);
        return jsonArray;
    }

    @Transactional
    @Override
    public void addKnowledge(Integer hospitalId, String headline, String content, List<String> imgPathList) {
        Knowledge knowledge = new Knowledge();
        knowledge.setHeadline(headline);
        knowledge.setContent(content);
        knowledge.setHospitalId(hospitalId);
        knowledge.setStatus("use");
        knowledgeRepository.save(knowledge);
        for (String imgPath : imgPathList) {
            KnowledgeImg knowledgeImg = new KnowledgeImg();
            knowledgeImg.setStatus("use");
            knowledgeImg.setKnowledgeId(knowledge.getId());
            knowledgeImg.setImgPath(imgPath);
            knowLedgeImgRepository.save(knowledgeImg);
        }
    }

    @Transactional
    @Override
    public void deleteKnowledge(Integer hospitalId, Integer knowledgeId) {

        Knowledge knowledge = knowledgeRepository.findOne(knowledgeId);
        if (knowledge.getHospitalId() != hospitalId) {

            return;
        } else {
            knowledge.setStatus("overdue");
            knowledgeRepository.save(knowledge);
            List<KnowledgeImg> knowledgeImgList = knowLedgeImgRepository.findByKnowledgeId(knowledgeId);
            for (KnowledgeImg knowledgeImg : knowledgeImgList) {
                knowledgeImg.setStatus("overdue");
                knowLedgeImgRepository.save(knowledgeImg);
            }
        }

    }

    @Override
    public JSONObject findById(Integer knowledgeId) {
        Knowledge knowledge = knowledgeRepository.findOne(knowledgeId);
        List<KnowledgeImg> knowledgeImgList = knowLedgeImgRepository.findByKnowledgeId(knowledgeId);
        KnowledgeDTOForSys knowledgeDTOForSys = new KnowledgeDTOForSys();
        knowledgeDTOForSys.setKnowledgeId(knowledgeId);
        knowledgeDTOForSys.setHeadline(knowledge.getHeadline());
        knowledgeDTOForSys.setContent(knowledge.getContent());
        knowledgeDTOForSys.setKnowledgeImgList(knowledgeImgList);
        System.out.println(knowledgeDTOForSys + "**********************");
        return JSONObject.fromObject(knowledgeDTOForSys);
    }

    @Transactional
    @Override
    public void updateKnowledge(Integer knowledgeId, String headline, String content) {
        Knowledge knowledge = knowledgeRepository.findOne(knowledgeId);
        knowledge.setHeadline(headline);
        knowledge.setContent(content);
        knowledgeRepository.save(knowledge);
    }
}
